package Util::Category::Category;
use Moose;
use namespace::autoclean;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub get_all {
	my ($self) = @_;
	return $self->schema->resultset('Category')->all;
}

sub create {
	my ($self, $cid, $name) = @_;
	my $category = $self->schema->resultset('Category')->create({
		cid => $cid,
		name => $name
	});
	return $category;
}

sub cats_total {
	my ($self) = @_;
	return $self->schema->resultset('Category')->search()->count;
}

sub get_cat_by_leimuid {
        my ($self, $leimuid) = @_;
	return $self->schema->resultset('Category')->search(
			{
				'leimu_category.leimu_id' => $leimuid},
			{
				join=> 'leimu_category'
			});

}

sub get_cat_by_cid{
        my ($self, $cid) = @_;
	return $self->schema->resultset('Category')->search(
			{
				'cid' => $cid},
			{});

}

sub get_cat_by_name {
        my ($self, $name) = @_;

        $name =~ s/^\s+//;
        $name =~ s/\s+$//;
	$name = "%" . $name . "%";
	return $self->schema->resultset('Category')->search(
		{name => { like => $name}},
		{},
	);
}


__PACKAGE__->meta->make_immutable;

1;
